/**
 * Copyright 2011 Honeywell Federal Manufacturing & Technologies, LLC. This material was produced under U.S. Government
 * contract DE-ACO4-01AL66850, Honeywell Federal Manufacturing & Technologies, LLC, (FM&T) with the U. S. Department of
 * Energy for the operation of the Kansas City Plant . The U.S. Government has rights to use, reproduce, and distribute
 * this software.  NEITHER THE GOVERNMENT NOR Honeywell Federal Manufacturing & Technologies, LLC, MAKES ANY WARRANTY,
 * EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE USE OF THIS SOFTWARE.  If software is modified to produce
 * derivative works, such modified software should be clearly marked, so as not to confuse it with the version available
 * from FM&T.
 *
 * Additionally, this program is free software; you can redistribute it and/or modify it under the terms of the GNU
 * General Public License Version 3 published by the Free Software Foundation. Accordingly, this program is distributed in
 * the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this library.
 * If not, see http://www.gnu.org/licenses/.
 */
package org.braintrain.tapestry.view;

import org.apache.tapestry.IAsset;
import org.apache.tapestry.IExternalPage;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.annotations.Asset;
import org.apache.tapestry.annotations.InjectPage;
import org.apache.tapestry.annotations.Persist;
import org.braintrain.domain.Navigation;

/**
 * The base page for pretty much every editable navigation page
 * 
 * @author John Ericksen
 *
 */
public abstract class BrainTrainEditableNavigatablePage extends BrainTrainSecuredBasePage implements IExternalPage{
    
    //Image Assets
    @Asset("/images/add.gif")
    public abstract IAsset getAddImage();    
    
    @Asset("/images/edit.gif")
    public abstract IAsset getEditImage(); 
    
    @Asset("/images/viewDifference.gif")
    public abstract IAsset getViewDifference();
    
    @Asset("/images/checked.gif")
    public abstract IAsset getCheckedImage();
    
    //Page Injections
    @InjectPage("View")
    public abstract View getViewPage();
    
    @InjectPage("Test")
    public abstract Test getTestPage();
    
    @InjectPage("EditTest")
    public abstract EditTest getEditTestPage();
    
    @InjectPage("ModuleAdmin")
    public abstract ModuleAdmin getModuleAdminPage();
    
    @InjectPage("Home")
    public abstract Home getHomePage();
    
    @InjectPage("Evaluation")
    public abstract Evaluation getEvaluationPage();
    
    public abstract Long getParentNavId();
    public abstract void setParentNavId(Long parentId);
    
    public abstract Long getNavId();
    public abstract void setNavId(Long navId);


    @Persist("client")
    public abstract Long getEditableKey();
    public abstract void setEditableKey(Long key);
    
    /**
     * Returns the current navigation
     * @return
     */
    public Navigation getNavigation()
    {
        Navigation currentNavigation = getGenericService().getById(Navigation.class, getNavId());
        
        if(currentNavigation == null)
        {
            Home home = getHomePage();
            if(!home.getDefaultDelegate().getHasErrors())
            {
                home.getDefaultDelegate().record(null, "Page could not be found.  You are probably seeing this error because the page you were navigating to was deleted.  Please retry or contact help support.");
            }
            getRequestCycle().activate(home);
            
        }
        return currentNavigation;
    }
    
    /**
     * Returns the parent navigation page
     * @return
     */
    public Navigation getParentNavigation()
    {
        if(getParentNavId() != null)
        {
            return getGenericService().getById(Navigation.class, getParentNavId());
        }
        return null;
    }
    
    public abstract void init();
    
    /**
     * Activate and Initalize the current page
     * @param cycle
     * @param id
     * @param parentId
     */
    public void activate(IRequestCycle cycle, Long id, Long parentId) {
        setParentNavId(parentId);
        setNavId(id);
        init();
        
        cycle.activate(this);
    }
    //activate externally by originalId, parentNavOriginalId
    public void activateExternalPage(Object[] params, IRequestCycle cycle) {
        if(params != null && params.length > 0 && params[0] instanceof Long )
        {
            setNavId((Long)params[0]);
            if(params.length > 1 && params[1] instanceof Long)
            {
                setParentNavId((Long)params[1]);
            }
            else
            {
                setParentNavId(null);
            }
            
            init();
            
            Navigation nav = getNavigation();
            
            if( getBraintrainVisit().getUser() != null &&
                    !(isDeltaUser() || 
                            (nav.getModule().isUnrestrictedUserAccess() || 
                                    nav.getModule().getUsers().contains(getBraintrainVisit().getUser()))))
            {
                cycle.activate(getNotAuthorizedPage());
            }
        }
        else
        {
            cycle.activate(getHomePage());
        }
    }
    
    //  convenience function for grabbing the id from a navigation
    public Long nullCheckNavId(Navigation nav)
    {
        if(nav != null)
        {
            return nav.getId();
        }
        return null;
    }
    
    public boolean isNavigationDeleted()
    {
        return getNavigation().isDeleted();
    }
}
